package solitaire.deck;

public class Validator {

	public static boolean MayMoveToEmptySlot(Card card)
	{
		return card.getRank() == Rank.King;
	}
	
	public static boolean IsTopMost(CardCollection cards)
	{
		return cards.size() == 1;
	}
	
	public static boolean MayTurnCard(CardCollection cards)
	{
		return IsTopMost(cards);
	}
	
	public static boolean MayMoveCard(Card card)
	{
		return card.isTurned();
	}
	
	private static boolean MayBePlacesOnPile(Pile pile, CardCollection cards, boolean addHigher)
	{
		Card cardToBePlaced = cards.getFirst();
		if(cardToBePlaced == null)
			return false;
		
		CardCollection pileCards = pile.getCards();
		Card topMostCard = pileCards.getLast();
		if(topMostCard == null)
			return false;
		
		//Check same suit
		if(topMostCard.getSuit() != cardToBePlaced.getSuit())
			return false;
		
		if(addHigher)
		{
			//Check if next in line
			if(topMostCard.getRank().getNr() != cardToBePlaced.getRank().getNr()+1)
				return false;
		}
		else
		{
			//Check if next in line
			if(topMostCard.getRank().getNr()+1 != cardToBePlaced.getRank().getNr())
				return false;
		}

		return true;		
	}
	
	public static boolean MayBePlacedOnPlayPile(PlayPile pile, CardCollection cards)
	{
		if(pile == null || cards == null)
			return false;
		
		if(pile.getCards().size() == 0 && cards.getFirst().getRank() == Rank.King)
			return true;
		
		return MayBePlacesOnPile(pile, cards, true);
	}
	
	
	public static boolean MayBePlacedOnAcePile(AcesPile pile, CardCollection cards)
	{
		if(pile == null || cards == null)
			return false;
		
		//Only add if we have one card
		if(!IsTopMost(cards))
			return false;
		
//		Check if empty slot then we can allow aces
		if(pile.getCards().size() == 0 && cards.getFirst().getRank() == Rank.Ace)
			return true;
		
		return MayBePlacesOnPile(pile, cards, false);
	}
}
